package managedbean.FinancialStatus;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import merlion.frm.bpm.entity.CustomerPaymentRecord;
import merlion.frm.bpm.entity.Invoice;
import merlion.frm.bpm.entity.SupplierInvoice;
import merlion.frm.bpm.entity.SupplierPaymentRecord;
import merlion.frm.bpm.pojo.Transaction;
import merlion.frm.bpm.session.FinancialStatusSessionBeanLocal;



@ManagedBean(name = "fsManagerBean")
@ViewScoped
public class FinancialStatusManagerBean {

    @EJB
    private FinancialStatusSessionBeanLocal fsSessionBean;
    private Collection<Invoice> allInvoices;
    private Collection<SupplierInvoice> allSupplierInvoices;
    private Collection<Invoice> allPaidInvoices;
    private Collection<SupplierInvoice> allPaidSupplierInvoices;
    private Collection<Invoice> allUnpaidInvoices;
    private Collection<SupplierInvoice> allUnpaidSupplierInvoices;
    private Collection<CustomerPaymentRecord> allCPR;
    private Collection<SupplierPaymentRecord> allSPR;
    private Double cashTotal;
    private Double receivableTotal;
    private Double payableTotal;
    
    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
     
    public FinancialStatusManagerBean() {
        
    }
    
    @PostConstruct
    public void init(){
        
    }
    
    //--------------------------------------------------------------------------
    //                       Financial Status Manager Bean
    //--------------------------------------------------------------------------
    
    public Collection<Transaction> getAllTransactions(){
        
        Collection<Transaction> transactions = new ArrayList();
        
        ArrayList<SupplierInvoice> siList = new ArrayList<SupplierInvoice>(getAllSupplierInvoices());
        ArrayList<Invoice> iList = new ArrayList<Invoice>(getAllInvoices());
        ArrayList<CustomerPaymentRecord> cprList = new ArrayList<CustomerPaymentRecord>(getAllCPR());
        ArrayList<SupplierPaymentRecord> sprList = new ArrayList<SupplierPaymentRecord>(getAllSPR());
        
        cashTotal = (double) 0.0;
        receivableTotal = (double) 0.0;
        payableTotal = (double) 0.0;
        
        for(Invoice i: iList){
            Transaction t = new Transaction(i);
            transactions.add(t);
            receivableTotal += i.getTotal();
        }
        for(SupplierInvoice si: siList){
            Transaction t = new Transaction(si);
            transactions.add(t);
            payableTotal += si.getInvoiceAmount();
        }
        for(CustomerPaymentRecord cpr: cprList){
            Transaction t = new Transaction(cpr);
            transactions.add(t);
            receivableTotal -= cpr.getPaymentAmount();
            cashTotal += cpr.getPaymentAmount();
        }
        for(SupplierPaymentRecord spr: sprList){
            Transaction t = new Transaction(spr);
            transactions.add(t);    
            payableTotal -= spr.getPaymentAmount();
            cashTotal -= spr.getPaymentAmount();
        }
        cashTotal = roundTwoDecimals(cashTotal);
        receivableTotal = roundTwoDecimals(receivableTotal);
        payableTotal = roundTwoDecimals(payableTotal);
        
        return transactions;
    }
    
    private double roundTwoDecimals(double d) {
        DecimalFormat fourDForm = new DecimalFormat("#.####");
        return Double.valueOf(fourDForm.format(d));
    }
    
    //--------------------------------------------------------------------------
    //                           Getter and Setter
    //--------------------------------------------------------------------------

    public Collection<Invoice> getAllInvoices() {
        return fsSessionBean.getAllInvoice();
    }

    public void setAllInvoices(Collection<Invoice> allInvoices) {
        this.allInvoices = allInvoices;
    }

    public Collection<Invoice> getAllPaidInvoices() {
        return fsSessionBean.getAllPaidInvoice();
    }

    public void setAllPaidInvoices(Collection<Invoice> allPaidInvoices) {
        this.allPaidInvoices = allPaidInvoices;
    }

    public Collection<SupplierInvoice> getAllPaidSupplierInvoices() {
        return fsSessionBean.getAllPaidSupplierInvoice();
    }

    public void setAllPaidSupplierInvoices(Collection<SupplierInvoice> allPaidSupplierInvoices) {
        this.allPaidSupplierInvoices = allPaidSupplierInvoices;
    }

    public Collection<SupplierInvoice> getAllSupplierInvoices() {
        return fsSessionBean.getAllSupplierInvoice();
    }

    public void setAllSupplierInvoices(Collection<SupplierInvoice> allSupplierInvoices) {
        this.allSupplierInvoices = allSupplierInvoices;
    }

    public Collection<Invoice> getAllUnpaidInvoices() {
        return fsSessionBean.getAllUnpaidInvoice();
    }

    public void setAllUnpaidInvoices(Collection<Invoice> allUnpaidInvoices) {
        this.allUnpaidInvoices = allUnpaidInvoices;
    }

    public Collection<SupplierInvoice> getAllUnpaidSupplierInvoices() {
        return fsSessionBean.getAllUnpaidSupplierInvoice();
    }

    public void setAllUnpaidSupplierInvoices(Collection<SupplierInvoice> allUnpaidSupplierInvoices) {
        this.allUnpaidSupplierInvoices = allUnpaidSupplierInvoices;
    }

    public Collection<CustomerPaymentRecord> getAllCPR() {
        return fsSessionBean.getAllCustomerPaymentRecord();
    }

    public void setAllCPR(Collection<CustomerPaymentRecord> allCPR) {
        this.allCPR = allCPR;
    }

    public Collection<SupplierPaymentRecord> getAllSPR() {
        return fsSessionBean.getAllSupplierPaymentRecord();
    }

    public void setAllSPR(Collection<SupplierPaymentRecord> allSPR) {
        this.allSPR = allSPR;
    }

    public Double getCashTotal() {
        return cashTotal;
    }

    public void setCashTotal(Double cashTotal) {
        this.cashTotal = cashTotal;
    }

    public Double getPayableTotal() {
        return payableTotal;
    }

    public void setPayableTotal(Double payableTotal) {
        this.payableTotal = payableTotal;
    }

    public Double getReceivableTotal() {
        return receivableTotal;
    }

    public void setReceivableTotal(Double receivableTotal) {
        this.receivableTotal = receivableTotal;
    }
    
}
